package com.gdssoft.system;

import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import junit.framework.TestCase;
import org.junit.Before;
import org.junit.Test;
import org.springframework.test.annotation.Rollback;

import com.gdssoft.BaseTestCase;
import com.gdssoft.core.command.QueryFilter;
import com.gdssoft.core.exception.ExistException;
import com.gdssoft.core.exception.NotCompleteException;
import com.gdssoft.core.exception.NotExistException;
import com.gdssoft.core.model.CheckCompleteResult;
import com.gdssoft.framework.model.system.AppUser;
import com.gdssoft.framework.model.system.Department;
import com.gdssoft.framework.model.system.Employee;
import com.gdssoft.framework.service.system.DepartmentService;
import com.gdssoft.framework.service.system.EmployeeService;

public class EmployeeServiceTestCase extends BaseTestCase {

	@Resource
	private EmployeeService employeeService;
	
	@Resource
	private DepartmentService departmentService;
	
	private Department defaultDept;
	
	@Before
	public void beforeTest() {
		defaultDept = departmentService.get(new Long(-1));
	}

	@Test
	@Rollback(true)
	public void testEmployeeService() {
		Employee entity = new Employee();
		//测试检查完整性
		CheckCompleteResult result = entity.checkComplete();
		//必须有检查完整性的结果对象
		TestCase.assertNotNull("check result should't Null", result);
		//因信息不完整，检查结果为假
		TestCase.assertFalse("check result should be False", result.isComplete());
		
		//填充必要信息
		entity.setEmpCode("sysDefault");
		entity.setFullname("系统默认用户");
		entity.setDepartment(defaultDept);
		entity.setBirthday(new Date("1888/08/08"));
		entity.setSex((short)1);
		entity.setAccessionDate(new Date("2005/03/25"));
		
		//再次检查完整性
		CheckCompleteResult result2 = entity.checkComplete();
		//检查结果应为真
		TestCase.assertTrue("check result should be True", result2.isComplete());
		
		//新增员工
		Employee employee = null;
		try {
			employee = employeeService.save(entity);
			TestCase.fail("Should not be successful");
		} catch (NotCompleteException e) {
			TestCase.fail("Should not be an exception");
		} catch (ExistException e) {
			//expected 因工号已存在，新增员工失败
		} catch (NotExistException e) {
			TestCase.fail("Should not be an exception");
		}
		//返回结果为Null
		TestCase.assertNull("employee should be Null", employee);

		//修改成一个未使用的工号，再次新增
		entity.setEmpCode("TEST0000");
		entity.setFullname("单元测试");
		try {
			employee = employeeService.save(entity);
		} catch (NotCompleteException e) {
			TestCase.fail("Should not be an exception");
		} catch (ExistException e) {
			TestCase.fail("Should not be an exception");
		} catch (NotExistException e) {
			TestCase.fail("Should not be an exception");
		}
		//新增员工成功，返回结果不为Null
		TestCase.assertNotNull("employee should't Null", employee);
		
		//利用getAll方法重新获取员工对象
		QueryFilter filter = new QueryFilter(0, Integer.MAX_VALUE);
		filter.addFilter("Q_empCode_S_EQ", "TEST0000");
		List<Employee> list = employeeService.getAll(filter);
		//结果应该只有一条
		TestCase.assertTrue("should be True", list.size()==1);
		
		Employee newEmployee = list.get(0);
		
		//返回结果不为Null
		TestCase.assertNotNull("newEmployee should't Null", newEmployee);
		//工号应该是TEST0000
		TestCase.assertEquals("EmpCode should be TEST0000", "TEST0000", newEmployee.getEmpCode());
		//姓名应该是单元测试
		TestCase.assertEquals("Fullname should be 单元测试", "单元测试", newEmployee.getFullname());
		//所在部门的名称应该是系统使用
		TestCase.assertEquals("Department Name should be 系统使用", "系统使用", newEmployee.getDepartment().getDepName());
		//职位应该为Null
		TestCase.assertNull("Positio should be Null", newEmployee.getPosition());
		
		//测试更新
		newEmployee.setFullname("单元测试2");
		newEmployee.setPosition("测试员");
		Employee doUpdateEmployee = null;
		try {
			doUpdateEmployee = employeeService.save(newEmployee);
		} catch (NotCompleteException e) {
			TestCase.fail("Should not be an exception");
		} catch (ExistException e) {
			TestCase.fail("Should not be an exception");
		} catch (NotExistException e) {
			TestCase.fail("Should not be an exception");
		}
		//返回结果不为Null
		TestCase.assertNotNull("doUpdateEmployee should't Null", doUpdateEmployee);
		//更新前后对象ID相等
		TestCase.assertTrue("should be True", newEmployee.getId().equals(doUpdateEmployee.getId()));
		//姓名应该是单元测试2
		TestCase.assertEquals("Fullname should be 单元测试2", "单元测试2", doUpdateEmployee.getFullname());
		//职位应该是测试员
		TestCase.assertEquals("Position should be 测试员", "测试员", doUpdateEmployee.getPosition());
		
		
		//测试移除
		Long employeeId = doUpdateEmployee.getId();
		try {
			employeeService.remove(employeeId);
		} catch (NotExistException e) {
			TestCase.fail("Should not be an exception");
		}
		//利用get方法重新获取员工对象
		Employee doRemoveEmployee = employeeService.get(employeeId);
		//返回结果为Null
		TestCase.assertNull("doRemoveEmployee should be Null", doRemoveEmployee);
		
		
		//再次测试更新一个已删除的对象
		doUpdateEmployee.setFullname("单元测试3");
		try {
			employeeService.save(doUpdateEmployee);
			TestCase.fail("Should not be successful");
		} catch (NotCompleteException e) {
			TestCase.fail("Should not be an exception");
		} catch (NotExistException e) {
			//expected 已删除的对象
		} catch (ExistException e) {
			TestCase.fail("Should not be an exception");
		}
	}
	
	/*
	@Test
	public void test() {
		//Employee employee = employeeService.getByEmpCode("F3201252");
		//Set<AppUser> users = employee.getAppUsers();
		//TestCase.assertNotNull("should't Null", users);
		//TestCase.assertEquals("result number should be 3", 3, users.size());
		
		try {
			employeeService.remove(50L);
		} catch (NotExistException e) {
			e.printStackTrace();
			TestCase.fail("Should not be an exception");
		}
	}
	*/
}